﻿using Authority.Logic;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Authority.Model;
using System.Collections.Generic;
using Tool.Model;
using System;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Network.Library;
using Ioc.Library;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Admin.Controllers
{
    /// <summary>
    /// 主界面操作
    /// </summary>
    [Area("Admin")]
    public class HomeController : BaseController
    {

        private IHostingEnvironment hostingEnv;

        public HomeController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
        }

        #region 界面
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult Index()
        {
            var resultInfo = new ResultActionInfo();
            try
            {
                var loginUser = UserSession.GetLoginUser<SysUserInfo>();
                {
                    if (loginUser != null)
                    {

                        #region 在这里，刷新主页，每次都去数据库拉一次数据，保证是最新的数据。
                        ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();
                        var userInfo = sysUserService.LoadUserByLoginName(loginUser.LoginName);
                        if (userInfo.Code == ActionCodes.Success)
                        {
                            ViewBag.LoginUserAccount = userInfo.Data.sLoginName;
                            ViewBag.LoginUserName = userInfo.Data.sUserName;
                            ViewBag.PicUrl = userInfo.Data.sPicUrl;

                            loginUser.LoginName = userInfo.Data.sLoginName;
                            loginUser.UserName = userInfo.Data.sUserName;
                            loginUser.PicUrl = userInfo.Data.sPicUrl;

                            UserSession.AddLoginUser(loginUser);
                        }
                        else
                        {
                            ViewBag.LoginUserAccount = loginUser.LoginName;
                            ViewBag.LoginUserName = loginUser.UserName;
                            ViewBag.PicUrl = loginUser.PicUrl;
                        }
                        var sysMenu = UnityCIContainer.Instance.GetService<ISysMenuService>();
                        var menusInfo = sysMenu.LoadMenusByRoles().Data.ToList();
                        #endregion

                        #region 保存当前用所拥有的按钮权限到Redis
                        var sysButton = UnityCIContainer.Instance.GetService<ISysButtonService>();
                        sysButton.AddButtonsCacheToRedis(loginUser.RoleCodes);
                        #endregion

                        ViewBag.Menus = menusInfo.FindAll(p => p.iLevel == (int)MenuLevel.Second);
                    }
                    else
                    {
                        resultInfo.RedirectPath = "/Admin/Login/Index";
                    }
                }
                resultInfo.Code = ActionCodes.Success;
                resultInfo.Msg = "获取成功！";
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnAction(resultInfo);
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult HomeMain()
        {
            return View();
        }

        /// <summary>
        /// 用户信息界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UserInfo()
        {
            return View();
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImg()
        {
            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult ChangePwd()
        {
            return View();
        }
        #endregion

        #region 主界面菜单逻辑方法

        /// <summary>
        /// 获取左侧菜单信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ContentResult LoadSubMenus()
        {
            var resultInfo = new ResultJsonInfo<List<MenuInfo>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                var sysMenu = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = sysMenu.LoadSubMenus(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }

            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取用户可操作菜单对应的可操作按钮
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ContentResult LoadButtonsInfo()
        {
            var resultInfo = new ResultJsonInfo<SysUserRoleButtonsHtmlInfo>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                var service = UnityCIContainer.Instance.GetService<ISysButtonService>();

                resultInfo = service.LoadButtonsCacheFromRedis(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }

            return ReturnContent(resultInfo);
        }
        #endregion

        #region 用户信息操作

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadUserInfo()
        {
            var resultInfo = new ResultJsonInfo<SysUserInfo>();
            try
            {
                var loginUser = UserSession.GetLoginUser<SysUserInfo>();
                resultInfo.Data = loginUser;
                resultInfo.Code = ActionCodes.Success;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult UploadImage()
        {
            var resultInfo = new ResultJsonInfo<string>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<Dictionary<string, object>>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = sysUserService.UploadImage(hostingEnv.WebRootPath, parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult SaveUserInfo()
        {
            var resultInfo = new ResultJsonInfo<SysUserEntity>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserInfoModifyRequest>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                resultInfo = sysUserService.SaveUserInfo(parametersInfo.parameters);

            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 确认用户修改密码
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult SaveChangePwd()
        {
            var resultInfo = new ResultJsonInfo<bool>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<UserPswModifyRequest>>();

                ISysUserService sysUserService = UnityCIContainer.Instance.GetService<ISysUserService>();

                var userInfo = UserSession.GetLoginUser<SysUserInfo>();

                parametersInfo.parameters.Code = userInfo.Code;

                resultInfo = sysUserService.SaveChangePwd(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}